package com.android.jay.lib_video.videoplayer.core;

import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.android.jay.lib_base.service.audio.AudioService;
import com.android.jay.lib_video.videoplayer.core.view.VideoFullDialog;
import com.android.jay.lib_video.videoplayer.utils.Utils;

/**
 * Created by wzp on 2020/2/6 0006.
 * <p>
 * 描述  ：  视频业务逻辑层
 */
public class VideoAdSlot implements CustomVideoView.ADVideoPlayerListener {

    private Context mContext;
    /**
     * UI
     **/
    private CustomVideoView mVideoView;
    private ViewGroup mParentView;


    /**
     * data
     **/
    @Autowired(name = "/audio/audio_service")
    public AudioService mAudioService; //private 注解找不到
    private String mVideoUrl;

    private SDKSlotListener mSlotListener;

    public VideoAdSlot(String url, SDKSlotListener mSlotListener) {
        ARouter.getInstance().inject(this);
        this.mVideoUrl = url;
        this.mSlotListener = mSlotListener;
        mParentView = mSlotListener.getAdParent();
        mContext = mParentView.getContext();
        initVideoView();
    }

    private void initVideoView() {
        mVideoView = new CustomVideoView(mContext);
        if (mVideoView != null) {
            mVideoView.setDataSource(mVideoUrl);
            mVideoView.setListener(this);
        }
        RelativeLayout paddingView = new RelativeLayout(mContext);
        paddingView.setBackgroundColor(mContext.getResources().getColor(android.R.color.black));
        paddingView.setLayoutParams(mVideoView.getLayoutParams());
        mParentView.addView(paddingView);
        mParentView.addView(mVideoView);
    }

    private boolean isRealPause() {
        if (mVideoView != null) {
            return mVideoView.ismIsRealPause();
        }
        return false;
    }

    private boolean isComplete() {
        if (mVideoView != null) {
            return mVideoView.ismIsComplete();
        }
        return false;
    }

    private void pauseVideo() {
        if (mVideoView != null) {
            mVideoView.pause();
        }
    }

    private void resumeVideo() {
        if (mVideoView != null) {
            mVideoView.start();
        }
    }

    public void destory() {
        mVideoView.destroy();
        mVideoView = null;
        mContext = null;
        mVideoUrl = null;
    }

    @Override
    public void onBufferUpdate(int time) {

    }

    @Override
    public void onClickFullScreenBtn() {
        Bundle bundle = Utils.getViewProperty(mParentView);
        mParentView.removeView(mVideoView); //从容器移除 一个空间只能一个父容器
        VideoFullDialog dialog = new VideoFullDialog(mContext, mVideoView, mVideoUrl,
                mVideoView.getCurrentPosition());
        dialog.setListener(new VideoFullDialog.FullToSmallListener() {
            @Override
            public void getCurrentPlayPosition(int position) {
                //回到小屏
                backToSmallMode(position);

            }

            @Override
            public void playComplete() {
                //大屏播放完毕
                bigPlayComplete();

            }
        });
        dialog.setSlotListener(mSlotListener);
        dialog.setViewBundle(bundle);
        dialog.show();
        //全屏暂停音乐播放
        mAudioService.pauseAudio();

    }

    private void bigPlayComplete() {
        if (mVideoView.getParent() == null) {
            mParentView.addView(mVideoView);
        }
        mVideoView.isShowFullBtn(true);
        mVideoView.mute(true);
        mVideoView.setListener(this);
        mVideoView.seekAndPause(0);
        mAudioService.resumeAudio();
    }

    private void backToSmallMode(int pos) {
        if (mVideoView.getParent() == null) {
            mParentView.addView(mVideoView);
        }
        mVideoView.isShowFullBtn(true);
        mVideoView.mute(true);
        mVideoView.setListener(this);
        mVideoView.seekAndResume(pos);
    }

    @Override
    public void onClickVideo() {

    }

    @Override
    public void onClickBackBtn() {

    }

    @Override
    public void onClickPlay() {

    }

    @Override
    public void onAdVideoLoadSuccess() {
        if (mSlotListener != null) {
            mSlotListener.onVideoLoadSuccess();
        }

    }

    @Override
    public void onAdVideoLoadFailed() {
        if (mSlotListener != null) {
            mSlotListener.onVideoFailed();
        }
    }

    @Override
    public void onAdVideoLoadComplete() {
        if (mSlotListener != null) {
            mSlotListener.onVideoComplete();
        }
        mVideoView.setmIsRealPause(true);
    }

    public interface SDKSlotListener {
        ViewGroup getAdParent();

        void onVideoLoadSuccess();

        void onVideoFailed();

        void onVideoComplete();
    }
}
